/*
 * #newUserNameField   #newPasswordField  #nameField   #identityNumberField   #emailField
 * max 70           max 70          max 100      max 11                 max 60
 */

function isEmail(value) {
    return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value);
}

function isNumeric(num) {
    return !isNaN(num);
}

function clean() {
    $('#newUserNameField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#newPasswordField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#nameField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#identityNumberField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#emailField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#countryDropDown').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#addressDropDown').parent().removeClass('has-error has-feedback').find('span').remove();

    $('#numberSeatsField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#plateNumberField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#brandField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#yearField').parent().removeClass('has-error has-feedback').find('span').remove();
}

function deepClean() {
    clean();
    $('#newUserNameField').val("");
    $('#newPasswordField').val("");
    $('#nameField').val("");
    $('#identityNumberField').val("");
    $('#emailField').val("");

    $('#numberSeatsField').val("");
    $('#plateNumberField').val("");
    $('#brandField').val("");
    $('#yearField').val("");
}

function validate() {
    clean();
    var validator = {valid: false};
    var newUserNameField = $('#newUserNameField').val();
    if (!newUserNameField) {
        $('#newUserNameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#newUserNameField');
        return validator;
    }
    if (newUserNameField.length > 70) {
        $('#newUserNameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#newUserNameField');
        return validator;
    }


    var newPasswordField = $('#newPasswordField').val();
    if (!newPasswordField) {
        $('#newPasswordField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Password must have 1-70 characters!</span>').insertAfter('#newPasswordField');
        return validator;
    }
    if (newPasswordField.length > 70) {
        $('#newPasswordField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Password must have 1-70 characters!</span>').insertAfter('#newPasswordField');
        return validator;
    }


    var nameField = $('#nameField').val();
    if (!nameField) {
        $('#nameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Name must have 1-100 characters!</span>').insertAfter('#nameField');
        return validator;
    }
    if (nameField.length > 70) {
        $('#nameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Name must have 1-100 characters!</span>').insertAfter('#nameField');
        return validator;
    }


    var identityNumberField = $('#identityNumberField').val();
    if (!identityNumberField) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }
    if (identityNumberField.length > 11 || identityNumberField.length < 8) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }
    if (!isNumeric(identityNumberField)) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }

    var emailField = $('#emailField').val();
    if (!emailField) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Email must have 1-60 characters!</span>').insertAfter('#emailField');
        return validator;
    }
    if (emailField.length > 70) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Email must have 1-60 characters!</span>').insertAfter('#emailField');
        return validator;
    }
    if (!isEmail(emailField)) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid Email adress!</span>').insertAfter('#emailField');
        return validator;
    }

    if ($("#countryDropDown").val() === "-1") {
        $('#countryDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid Country!</span>').insertAfter('#countryDropDown');
        return validator;
    }

    if ($("#addressDropDown").val() === "-1") {
        $('#addressDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid address!</span>').insertAfter('#addressDropDown');
        return validator;
    }


    var numberSeatsField = $('#numberSeatsField').val();
    if (!numberSeatsField) {
        $('#numberSeatsField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Number of seats must have 2-11 numbers!</span>').insertAfter('#numberSeatsField');
        return validator;
    }
    if (numberSeatsField.length > 2 || numberSeatsField.length < 1) {
        $('#numberSeatsField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Number of seats must have 1-2 numbers!</span>').insertAfter('#numberSeatsField');
        return validator;
    }
    if (!isNumeric(numberSeatsField)) {
        $('#numberSeatsField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Number of seats must have 2-11 numbers!</span>').insertAfter('#numberSeatsField');
        return validator;
    }

    var plateNumberField = $('#plateNumberField').val();
    if (!plateNumberField) {
        $('#plateNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Plate number must have 1-10 characters!</span>').insertAfter('#plateNumberField');
        return validator;
    }
    if (plateNumberField.length > 10) {
        $('#plateNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Plate number must have 1-10 characters!</span>').insertAfter('#plateNumberField');
        return validator;
    }

    var brandField = $('#brandField').val();
    if (!brandField) {
        $('#brandField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Brand must have 1-70 characters!</span>').insertAfter('#brandField');
        return validator;
    }
    if (brandField.length > 70) {
        $('#brandField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Brand must have 1-70 characters!</span>').insertAfter('#brandField');
        return validator;
    }

    var yearField = $('#yearField').val();
    if (!yearField) {
        $('#yearField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Year must have 4 numbers!</span>').insertAfter('#yearField');
        return validator;
    }
    if (yearField.length !== 4) {
        $('#yearField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Year must have 4 numbers!</span>').insertAfter('#yearField');
        return validator;
    }
    if (!isNumeric(yearField)) {
        $('#yearField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Year must have 4 numbers!</span>').insertAfter('#yearField');
        return validator;
    }

    validator["userNameField"] = newUserNameField;
    validator["passwordField"] = newPasswordField;
    validator["nameField"] = nameField;
    validator["identityNumberField"] = identityNumberField;
    validator["emailField"] = emailField;
    validator["id_country"] = $("#countryDropDown").val();
    validator["id_address"] = $("#addressDropDown").val();

    validator["numberSeatsField"] = numberSeatsField;
    validator["plateNumberField"] = plateNumberField;
    validator["brandField"] = brandField;
    validator["yearField"] = yearField;

    var welcomeMessageField = $('#welcomeMessageField').val();
    if (!welcomeMessageField) {
        welcomeMessageField = ' ';
    }
    validator["welcomeMessageField"] = welcomeMessageField;

    validator["valid"] = true;
    return validator;
}

function send(validator) {
    sendRegisterUser(validator, function (data) {
        if (data["success"] === true) {
            window.location = '/Trabalho_PD/login.html';
        } else {
            $("#registerUserButtonArea").append('<span class="glyphicon glyphicon-remove form-control-feedback"></span><span class="help-block">Error! Retry please!</span>');
        }
    });
}

function sendNewUser() {
    var validator = validate();
    if (validator["valid"]) {
        send(validator);
    }
}

function loadCountrys() {
    getAllCountrys(function (arrayCountry) {
        for (var i in arrayCountry) {
            $("#countryDropDown").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
        }
    });
}

$(document).ready(function () {
    loadCountrys();
    $("#countryDropDown").change(function () {
        if ($("#countryDropDown").val() !== "-1") {
            getAllAdressFromCountry($("#countryDropDown").val(), function (arrayAdress) {

                $('#addressDropDown')
                        .find('option')
                        .remove()
                        .end()
                        .append('<option value="-1">Select a address</option>');

                for (var i in arrayAdress) {
                    $("#addressDropDown").append('<option value="' + arrayAdress[i].id_address + '">' + arrayAdress[i].address + '</option>');
                }
            });
        }
    });
});


